import entApi from '@/api/modules/ent'
import documentApi from '@/api/modules/document'
import assetsApi from '@/api/modules/assets'
import userApi from '@/api/modules/user'
import updateApi from '@/api/modules/update'
import dataQueryApi from '@/api/modules/data.query'
import type { DataList, EntDevices, TreeData } from '@/api/modules/types/ent'

export function changeChildren(arr: any[]) {
  arr.forEach((item) => {
    if (item.Children) {
      item.children = item.Children
      item.label = item.Name
      item.value = item.Code
      changeChildren(item.children)
    }
  })
  return arr
}

export function useGetNEIndustrys() {
  const data = ref<TreeData[]>([])

  entApi.GetNEIndustrys().then((res) => {
    data.value = changeChildren(res)
  })

  return data
}

export function useOwnerOrgsTree() {
  const list = ref<TreeData[]>([])
  entApi.GetOwnerOrgs().then((res) => {
    list.value = changeChildren(res)
  })
  return list
}

export function useGetDistricts() {
  const data = ref<any[]>([])

  entApi.GetDistricts().then((res) => {
    data.value = changeChildren(res)
  })

  return data
}

export function getEntInfo(enterpriseID: string) {
  return entApi.GetEnterprises(
    {
      objFilter: {
        Type: 'DIM.Units.Filters.GUIDFilter',
        Value: [enterpriseID],
      },
      pageSize: 0,
      pageNum: 0,
    }).then((res) => {
    const { Objs } = res
    return Objs[0]
  })
}

// 获取用户账户
export function useAccount(enterpriseObjID: string) {
  const accountData = reactive({
    data: [{ AccountCode: '', Password: '' },
      { AccountCode: '', Password: '' },
      { AccountCode: '', Password: '' }],
  })

  enterpriseObjID && entApi.GetAccount({ enterpriseObjID }).then((res) => {
    if (Array.isArray(res)) {
      res.forEach((data, index) => {
        accountData.data[index] = data
      })
    }
  })

  return accountData
}

export function useGetProduceReuse(enterpriseObjID: string) {
  const data = reactive<EntDevices>({
    ElectricDevices: [],
    Produces: {},
    Reuses: {},
    AllReuses: {},
  })

  entApi.GetProduceReuse({ enterpriseObjID }).then((res) => {
    data.ElectricDevices = res.ElectricDevices
    data.Produces = res.Produces
    data.Reuses = res.Reuses
  })

  return data
}

export function getMNCode(enterpriseObjID: string) {
  return entApi.CreateMN({ enterpriseObjID }).then((res) => {
    return res
  })
}

export function getPointCode(parentObjID: string) {
  return entApi.CreatePointCode({ parentObjID }).then((res) => {
    return res
  })
}

export function useOwnerOrgs() {
  const list = ref<TreeData[]>([])
  entApi.GetOwnerOrgs().then((res) => {
    list.value = res
  })
  return list
}

export function useEnvOrgs() {
  const list = ref<TreeData[]>([])
  entApi.GetEnvOrgs().then((res) => {
    list.value = res
  })
  return list
}
export function useDeviceStates() {
  const list = ref<DataList>([])
  entApi.GetDeviceStates().then((res) => {
    list.value = res
  })
  return list
}

export function useClassifys() {
  const list = ref<any>({})
  documentApi.GetOwnerOrgClassifys().then((res) => {
    list.value = res
  })
  return list
}

export function useAssetFactorys() {
  const list = ref<any>({})
  assetsApi.GetAssetFactorys().then((res) => {
    list.value = res
  })
  return list
}
export function useAssetModelClassifys() {
  const list = ref<any>({})
  assetsApi.GetAssetModelClassifys().then((res) => {
    list.value = res
  })
  return list
}
export function useAssetModels() {
  const list = ref<any>({})
  assetsApi.GetAssetModels().then((res) => {
    list.value = res
  })
  return list
}
export function useStateTypes() {
  const list = ref<any>({})
  assetsApi.GetStateTypes().then((res) => {
    list.value = res
  })
  return list
}

export function useRoles() {
  const list = ref<any>([])
  userApi.GetRoleList().then((res) => {
    list.value = res
  })
  return list
}

export function flatTreeData(arr: any[]) {
  arr.forEach((item) => {
    if (item.Children) {
      item.children = item.Children
      if (Array.isArray(item.LogTypes) && item.LogTypes.length) {
        item.children = item.children.concat(item.LogTypes.map((Type: any) => ({ value: Type.Code, label: Type.Name, isType: true })))
      }
      item.label = item.Name
      item.value = item.Code
      flatTreeData(item.children)
    }
  })
  return arr
}

export function useLogClassify() {
  const list = ref<any>([])
  entApi.GetLogClassifys().then((res) => {
    list.value = flatTreeData(res)
  })
  return list
}

export function useUpdateStates() {
  const list = ref<any>([])
  updateApi.GetUpgradeStates().then((res) => {
    list.value = Object.keys(res).forEach((key) => {
      return {
        value: key,
        label: res[key],
      }
    })
  })
  return list
}

export function useDeclareStates() {
  const list = ref<any>([])
  dataQueryApi.GetExDeclareStates().then((res) => {
    list.value = res
  })
  return list
}
